package cn.player.act;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

import cn.player.R;
import cn.player.act.frag.ItemFragment;
import cn.player.adapter.CommonAdapter;
import cn.player.adapter.TabFragmentPagerAdapter;
import cn.player.entity.MenuItem;
import cn.player.view.SlidingMenuView;
import cn.player.view.TabChangedColorView;
import cn.player.view.ViewHolder;
import cntv.player.core.util.ToastUtil;
import cntv.player.core.util.Utils;

public class MainSlidingMenuActivity extends BaseActivity implements View.OnClickListener, ViewPager.OnPageChangeListener, AdapterView.OnItemClickListener {

    private LinearLayout llLeftMenu;
    private RelativeLayout rlLeftTop;
    private ListView lvLeftMenuList;
    private SlidingMenuView mSlidingMenuView;

    private LinearLayout llMainContent;
    private RelativeLayout rlTitleArea;
    private ImageView ivToggleMenu;
    private ViewPager vpMainContent;
    private LinearLayout llBottomTab;
    private TabChangedColorView tabMessage;
    private TabChangedColorView tabFriendsList;
    private TabChangedColorView tabFind;
    private TabChangedColorView tabMine;

    private List<TabChangedColorView> mTabChangedViews;
    private List<MenuItem> menuItems;
    private List<Fragment> mFragments;
    private TabFragmentPagerAdapter mPageAdapter;


    public static final int TAB_MESSAGE = 0;
    public static final int TAB_FRIENDS_LIST = 1;
    public static final int TAB_FIND = 2;
    public static final int TAB_MINE = 3;
    private int mCurrentTab = TAB_MESSAGE;
    private boolean mClickTab;


    protected void findViews() {
        llLeftMenu = (LinearLayout) findViewById(R.id.ll_left_menu);
        rlLeftTop = (RelativeLayout) findViewById(R.id.rl_left_top);
        lvLeftMenuList = (ListView) findViewById(R.id.lv_left_menu_list);

        llMainContent = (LinearLayout) findViewById(R.id.ll_main_content);
        ivToggleMenu = (ImageView) findViewById(R.id.iv_toggle_menu);
        rlTitleArea = (RelativeLayout) findViewById(R.id.rl_title_area);
        vpMainContent = (ViewPager) findViewById(R.id.vp_main_content);


        mSlidingMenuView = (SlidingMenuView) findViewById(R.id.smv_menu_layout);

        llMainContent = (LinearLayout) findViewById(R.id.ll_main_content);
        rlTitleArea = (RelativeLayout) findViewById(R.id.rl_title_area);
        ivToggleMenu = (ImageView) findViewById(R.id.iv_toggle_menu);
        vpMainContent = (ViewPager) findViewById(R.id.vp_main_content);
        llBottomTab = (LinearLayout) findViewById(R.id.ll_bottom_tab);
        tabMessage = (TabChangedColorView) findViewById(R.id.tab_message);
        tabFriendsList = (TabChangedColorView) findViewById(R.id.tab_friends_list);
        tabFind = (TabChangedColorView) findViewById(R.id.tab_find);
        tabMine = (TabChangedColorView) findViewById(R.id.tab_mine);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_sliding_menu);
        findViews();
        setListeners();
        setStatusBar();
        setHeaderLayout();
        initMenuData();
        initViewPager();
        initData();
    }

    private void initData() {
        mTabChangedViews = new ArrayList<>();
        mTabChangedViews.add(tabMessage);
        mTabChangedViews.add(tabFriendsList);
        mTabChangedViews.add(tabFind);
        mTabChangedViews.add(tabMine);

        mTabChangedViews.get(0).setIconAlpha(1.0f);

    }

    private void setStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    /**
     * 设置透明状态栏时，增加头部高度
     */
    private void setHeaderLayout() {
        int statusBarHeight = Utils.getStatusBarHeight(this);
        if (statusBarHeight == 0) {
            statusBarHeight = getResources().getDimensionPixelOffset(R.dimen.default_status_bar_height);
        }
        int mainHeaderHeight = getResources().getDimensionPixelOffset(R.dimen.main_title_bar_height);

        ViewGroup.LayoutParams layoutParams = rlTitleArea.getLayoutParams();
        layoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            layoutParams.height = mainHeaderHeight + statusBarHeight;
        } else {
            layoutParams.height = mainHeaderHeight;
        }
        rlTitleArea.setLayoutParams(layoutParams);
    }


    private void initMenuData() {
        menuItems = new ArrayList<>();
        menuItems.add(new MenuItem("MenuItem1", R.mipmap.menu_img_1));
        menuItems.add(new MenuItem("MenuItem2", R.mipmap.menu_img_2));
        menuItems.add(new MenuItem("MenuItem3", R.mipmap.menu_img_3));
        menuItems.add(new MenuItem("MenuItem4", R.mipmap.menu_img_4));
        menuItems.add(new MenuItem("MenuItem5", R.mipmap.menu_img_5));

        lvLeftMenuList.setAdapter(new CommonAdapter<MenuItem>(this, menuItems, R.layout.slide_menu_item_layout) {
            @Override
            protected void convertView(ViewHolder viewHolder, MenuItem bean) {
                viewHolder.setImageResource(R.id.iv_menu_icon, bean.getResId())
                        .setText(R.id.tv_menu_title, bean.getTitle());
            }
        });
    }

    private void initViewPager() {
        mFragments = new ArrayList<>();
        mFragments.add(ItemFragment.newInstance(1));
        mFragments.add(ItemFragment.newInstance(2));
        mFragments.add(ItemFragment.newInstance(3));
        mFragments.add(ItemFragment.newInstance(4));

        mPageAdapter = new TabFragmentPagerAdapter(getSupportFragmentManager(), mFragments, null);
        vpMainContent.setAdapter(mPageAdapter);
    }

    protected void setListeners() {
        ivToggleMenu.setOnClickListener(this);
        tabMessage.setOnClickListener(this);
        tabFriendsList.setOnClickListener(this);
        tabFind.setOnClickListener(this);
        tabMine.setOnClickListener(this);

        vpMainContent.addOnPageChangeListener(this);
        lvLeftMenuList.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ToastUtil.showToast(this, menuItems.get(position).getTitle(), ToastUtil.LENGTH_SHORT);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_toggle_menu:
                mSlidingMenuView.toggleMenu();
                break;
            case R.id.tab_message:
                mClickTab = true;
                vpMainContent.setCurrentItem(TAB_MESSAGE);
                break;
            case R.id.tab_friends_list:
                mClickTab = true;
                vpMainContent.setCurrentItem(TAB_FRIENDS_LIST);
                break;
            case R.id.tab_find:
                mClickTab = true;
                vpMainContent.setCurrentItem(TAB_FIND);
                break;
            case R.id.tab_mine:
                mClickTab = true;
                vpMainContent.setCurrentItem(TAB_MINE);
                break;
            default:
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        //从第二页滑到第三页, position 1..~2(最后一次), positionOffset 0.0  ... 0.9999~ 0.0(最后一次)
        //从第三页滑到第二页, position 1..~1(最后一次), positionOffset 0.9999  ... 0.0~ 0.0(最后一次)

        //从第二页时，点击跳转至第四页, position 2..~3(最后一次), positionOffset 0.0  ... 0.9999~ 0.0(最后一次)
        //从第四页时，点击跳转至第二页, position 1..~1(最后一次), positionOffset 0.9999  ... 0.0~ 0.0(最后一次)
//        Log.i("zl", "onPageScrolled position = " + position + ",positionOffset = " + positionOffset);
        if (positionOffset > 0 && !mClickTab) {
            mTabChangedViews.get(position).setIconAlpha(1 - positionOffset);
            mTabChangedViews.get(position + 1).setIconAlpha(positionOffset);
        }

    }

    @Override
    public void onPageSelected(int position) {
        if (mClickTab) {
            Log.i("zl", "onPageSelected position = " + position);
            mTabChangedViews.get(mCurrentTab).setIconAlpha(0);
            mTabChangedViews.get(position).setIconAlpha(1);
        }
        mCurrentTab = position;

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        switch (state) {
            case ViewPager.SCROLL_STATE_IDLE:
                mClickTab = false;
                break;
        }
    }

}
